package com.ks.comment.service.impl;

import com.ks.comment.controller.dto.AgreeResponse;
import com.ks.comment.entity.ZpzAgree;
import com.ks.comment.dao.ZpzAgreeMapper;
import com.ks.comment.feign.IBCmsNewsService;
import com.ks.comment.service.IZpzAgreeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.comment.service._ServiceBase;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表结构公共字段 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-18
 */
@Service
public class ZpzAgreeServiceImpl extends _ServiceBase<ZpzAgreeMapper, ZpzAgree> implements IZpzAgreeService {

    @Resource
    private ZpzAgreeMapper zpzAgreeMapper;
    @Resource
    private IBCmsNewsService ibCmsNewsService;

    @Override
    public int queryAgreeStatus(OperationContext operationContext, int informationId, String type) {
        ZpzAgree agree = new ZpzAgree();
        agree.setInformationId(informationId);
        agree.setCType(type);
        agree.setIuserId(operationContext.getUserId().intValue());
        ZpzAgree agree1 = zpzAgreeMapper.selectOne(agree);
        if (ToolUtil.isNotEmpty(agree1)) {
            if (agree1.getDelMark() ==Const.DEL_FLAG_NORMAL){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int addAgree(OperationContext operationContext, int informationId, String type, int status) {
        ZpzAgree agree = new ZpzAgree();
        agree.setInformationId(informationId);
        agree.setCType(type);
        agree.setSStatus(status);
        agree.setIuserId(operationContext.getUserId().intValue());
        ZpzAgree zpzAgree = zpzAgreeMapper.selectOne(agree);
        if (ToolUtil.isNotEmpty(zpzAgree)) {
            if (zpzAgree.getDelMark() == Const.DEL_FLAG_NORMAL) {
                zpzAgree.setDelMark(Const.DEL_FLAG_DEL);
                Integer integer = zpzAgreeMapper.updateById(zpzAgree);
                return integer;
            }
            if (zpzAgree.getDelMark() == Const.DEL_FLAG_DEL){
                zpzAgree.setDelMark(Const.DEL_FLAG_NORMAL);
                Integer integer = zpzAgreeMapper.updateById(zpzAgree);
                return integer;
            }
        }
        agree.setTenantID(operationContext.getTenantId().intValue());
        agree.setOrgID(operationContext.getOrgId().intValue());
        agree.setOrgSubID(operationContext.getOrgSubId().intValue());
        agree.setCreateUserID(operationContext.getUserId().intValue());
        Integer insert = zpzAgreeMapper.insert(agree);
        if (insert > 0) {
            ibCmsNewsService.addCmsCount(agree.getInformationId(),Const.Cms.AGREE_TYPE);
            return insert;
        }
        return 0;
    }
    @Override
    public Map<String, Object> getAgreeList(int informationId, String type, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<AgreeResponse> agreeList = zpzAgreeMapper.getAgreeList(informationId, type, pageNo, pageSize);
        int agreeCount = zpzAgreeMapper.getAgreeCount(informationId, type);
        map.put("list", agreeList);
        map.put("count", agreeCount);
        return map;
    }
}
